﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using 挂号.compent;

namespace 挂号
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<ComboboxData> mycom = new List<ComboboxData>();
            mycom.Add(new ComboboxData() { id = "005005-020", name = "妇产科" });
            mycom.Add(new ComboboxData() { id = "005005-011", name = "儿科" });
            this.mycombo.ItemsSource = mycom;
            this.mycombo.SelectedIndex = 0;
        }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                this.DragMove();
            }
        }
        //最小化
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        //关闭程序
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //用户点击提交,
        private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (this.SFZH.Text.Length != 18)
            {
                MessageBox.Show("身份证号码长度不对");
                return;
            }

            if (MainWindow.CalculateCheckDigit(this.SFZH.Text.Substring(0, 17)) != this.SFZH.Text.Substring(17, 1))
            {
                MessageBox.Show("身份证号码输入不正确");
                return;
            }

            //MessageBox.Show($"科室:{this.mycombo.Text} 身份证号码:{this.SFZH.Text}");
            //     'name': '张思',
            //     'sex': "2",
            //     'birth': "1990-02-01",
            //     'address': "武穴",
            //     'cardNum': "321321198403207234",

            Http htp = new Http();
            try
            {
                PatientInfo res = await htp.Register(new PatientInfo()
                {
                    Name = "",
                    Sex = "",
                    birth = "",
                    address = "",
                    cardNum = this.SFZH.Text,
                    DeptId = this.mycombo.SelectedValue.ToString(),
                    DeptName = ((ComboboxData)this.mycombo.SelectedItem).name
                });
                this.patientid.Text = res.CardNo;
                MessageBox.Show($"叫号成功,患者姓名{res.Name},身份证号:{res.cardNum},挂号科室:{res.DeptName},患者就诊ID:{res.CardNo}");
            }
            catch (IndexOutOfRangeException)
            {
                MessageBox.Show("该身份证号未在系统登记,请到一楼挂号");
            }



        }

        public static string CalculateCheckDigit(string idWithoutCheckDigit)
        {
            if (idWithoutCheckDigit.Length != 17)
            {
                throw new ArgumentException("身份证号码必须为17位数字");
            }

            int[] weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };

            char[] codes = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
            int sum = 0;

            for (int i = 0; i < 17; i++)
            {
                if (!int.TryParse(idWithoutCheckDigit[i].ToString(), out int digit))
                {
                    throw new ArgumentException("身份证号码包含非数字字符");
                }
                sum += digit * weights[i];
            }

            int remainder = sum % 11;





            return codes[remainder].ToString();
        }
    }
}
